package com.example.widgetapplication;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

/*
实现步骤建议：
1.首先定义内部类ViewHolderR，注意类名与RecyclerView.ViewHolder类名的区别，免除后混淆烦恼；
2.然后实现MangheAdapterR类的继承设置RecyclerView.Adapter<MangheAdapterR.ViewHolderR>；
3.随后定义内部数据列表和MangheAdapterR构造方法；
4.最后实现所需的@Override方法。
*/

public class MangheAdapterR extends RecyclerView.Adapter<MangheAdapterR.ViewHolderR>{
    private List<Manghe> mMangheList;

    public MangheAdapterR(List<Manghe> mMangheList) {
        this.mMangheList = mMangheList;
    }
    /*onCreateViewHolder方法用于引入子项布局创建ViewHolderR实例，并将加载出的布局传入构造函数中，最后返回该实例中*/
    @NonNull
    @Override
    public ViewHolderR onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_manghe_item_v,parent,false);
        final  ViewHolderR holderR = new ViewHolderR(view);

        /*跳转页面，事件响应*/
        holderR.mangheView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position = holderR.getAdapterPosition();
                Context context = view.getContext();
                Intent intent = new Intent(context,MainActivity2.class);
                intent.putExtra("goodsId",position);
                context.startActivity(intent);

                Manghe manghe = mMangheList.get(position);
                Toast.makeText(view.getContext(),"You Click" + manghe.getName(),Toast.LENGTH_SHORT).show();
            }
        });
        return holderR;
    }
    /*onBindViewHolder方法用于RecyclerView子项的数据进行赋值，即将Manghe实例取出的图片和文本信息设置ViewHolderR中对应变量*/
    @Override
    public void onBindViewHolder(@NonNull ViewHolderR holder, int position) {
        Manghe manghe = mMangheList.get(position);
        holder.mangheImage.setImageResource(manghe.getImageId());
        holder.mangheName.setText(manghe.getName());
    }
    /*getItemCount方法用于告诉RecyclerView一共有多少个子项*/
    @Override
    public int getItemCount() {
        return mMangheList.size();
    }

    static class ViewHolderR extends RecyclerView.ViewHolder{
        View mangheView; /*整个子项布局*/
        ImageView mangheImage;
        TextView mangheName;

        public ViewHolderR(@NonNull View itemView) {
            super(itemView);
            mangheView = itemView;
            mangheImage = itemView.findViewById(R.id.manghe_image);
            mangheName = itemView.findViewById(R.id.manghe_name);
        }


    }

    ;
}
